package com.LeeCode;

import java.util.Arrays;

/**
 * 划分数组并满足最大差限制
 */

public class Code2966 {
    public static void main(String[] args) {
        int[] nums = {2, 4, 2, 2, 5, 2};
        int k = 2;
        System.out.println(Arrays.deepToString(new Code2966().divideArray(nums, k)));
    }

    public int[][] divideArray(int[] nums, int k) {
        int n = nums.length;
        int[][] ans = new int[n / 3][3];
        Arrays.sort(nums);

        for (int i = 0; i < n; i += 3) {
            if (nums[i + 2] - nums[i] > k) {
                return new int[0][];
            }
            int index = i / 3;
            ans[index][0] = nums[i];
            ans[index][1] = nums[i + 1];
            ans[index][2] = nums[i + 2];
        }

        return ans;
    }
}
